package me.flyray.bsin.server.impl;

import me.flyray.bsin.facade.service.MedicalAdviceService;
import me.flyray.bsin.server.utils.BsinInvoke;
import me.flyray.bsin.server.utils.RespBodyHandler;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MedicalAdviceServiceImpl implements MedicalAdviceService {

    /**
     * 服务任务
     * 1、根据流程变量中的病人id ,查询负责该病人的nurseId
     * 2、将nurseId 添加到流程变量
     */
    public Map<String,Object> allotTasks(Map<String,Object> requestMap)throws IOException {
        String medicalAdvice = (String)requestMap.get("medicalAdvice");
        String patientId = (String)requestMap.get("patientId");
        System.out.println("下发医嘱："+medicalAdvice);

        // 可以根据病人id 获取负责该病人的护士id
        Map<String,Object> result = new HashMap<>();
        result.put("nurseId","002");

        // 向 001号护士的腕表 推送消息
        Map<String,Object> bizParams = new HashMap<>();
        bizParams.put("userId","002");
        bizParams.put("msg",medicalAdvice);
        BsinInvoke.genericInvoke("BsinWebSocketService","sendTo",bizParams);

        return result;
    }

    /**
     * 任务的查看
     * @param requestMap （处理人 assignee）
     */
    public Map<String,Object> getTask(Map<String,Object> requestMap){
        String assignee = (String) requestMap.get("assignee");
        Map<String,Object> reParam = new HashMap<>();
        reParam.put("assignee",assignee);
        return BsinInvoke.genericInvoke("BsinTaskService", "getTaskByUser", reParam);
    }

    /**
     * 任务的转办
     * 护士A手上的未完成的任务，因为下班时间到了，转给护士B
     */
    public Map<String,Object> transferTask(Map<String,Object> requestMap){
        String taskId = (String) requestMap.get("taskId");
        String assignee = (String) requestMap.get("assignee");
        Map<String,Object> reParam = new HashMap<>();
        reParam.put("taskId",taskId);
        reParam.put("userId",assignee);
        Map<String,Object> map = (Map<String,Object>) BsinInvoke.genericInvoke("BsinTaskService", "transfer", reParam);
        String medicalAdvice = (String)map.get("medicalAdvice");
        // 向 001号护士的腕表 推送消息
        Map<String,Object> bizParams = new HashMap<>();
        bizParams.put("userId",assignee);
        bizParams.put("msg",medicalAdvice);
        BsinInvoke.genericInvoke("BsinWebSocketService","sendTo",bizParams);
        System.out.println("当前任务已流转给："+assignee);
        return RespBodyHandler.RespBodyDto();
    }

    /**
     * 任务完成
     */
    public Map<String,Object> complete(Map<String,Object> requestMap){
        String taskId = (String) requestMap.get("taskId");
        Map<String,Object> reParam = new HashMap<>();
        reParam.put("taskId",taskId);
        BsinInvoke.genericInvoke("BsinTaskService","complete",reParam);
        System.out.println("当前任务已完成");
        return RespBodyHandler.RespBodyDto();
    }


}
